﻿using System;
using System.ComponentModel;
using System.IO;
using System.Messaging;
using System.Text;
using System.Windows.Forms;
using MsgQueueTest.Data;
using MsgQueueTest.Entity;
using MsgQueueTest.Mail;
using MsgQueueTest.Message;

namespace MsgQueueTest.UI
{
    public partial class MainForm : Form
    {
        private MessageQueue messageQueue;
        private readonly ContentManager contentManager;

        public MainForm()
        {
            InitializeComponent();
            contentManager = new ContentManager();
            contentManager.Test();
        }

        private void MsgbackGroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            SetTitle("Running");
            while (true)
            {
                try
                {
                    var message = messageQueue.Receive();
                    if (message == null) break;
                    var content = (Email) message.Body;
                    var body = contentManager.GetMailBody(content);

                    //if(content.Destination.Equals("lance.zhou.suryani@gmail.com"))
                    //{
                    //    MailManager.SendMail(content.Destination,content.Destination,content.Subject,body);
                    //}
                    contentManager.SetEmailSended(int.Parse(content.NotifyID));

                    SetLog(string.Format("{0}:{1}\r\n", message.Label, content.TemplateName));

                    File.WriteAllText(string.Format("{0}/Emails/{1}.html",Application.StartupPath,content.Subject),body);
                }
                catch (MessageQueueException mqex)
                {

                }
                catch(Exception ex)
                {
                    
                }
            }
        }

        private void MsgbackGroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            SetTitle("Stopped");
            messageQueue.Close();
            messageQueue = null;
        }

        public void SetTitle(string title)
        {
            if (InvokeRequired)
            {
                Invoke(new SetTitleDelegate(SetTitle), new object[] {title});
            }
            else
            {
                Text = title;
            }
        }

        public void SetLog(string title)
        {
            if (InvokeRequired)
            {
                Invoke(new SetTitleDelegate(SetLog), new object[] {title});
            }
            else
            {
                LogTextBox.Text = LogTextBox.Text + title;
            }
        }


        private void StartButton_Click(object sender, EventArgs e)
        {
            if (messageQueue == null)
            {
                messageQueue = new MessageQueue(QueueTextBox.Text.Trim())
                                   {
                                       Formatter = new MyXmlMessageFormater(new[] {typeof (Email)}, Encoding.Unicode)
                                   };

                MsgbackGroundWorker.RunWorkerAsync();
            }
        }

        private void StopButton_Click(object sender, EventArgs e)
        {
            if (messageQueue != null)
            {
                messageQueue.Close();
            }
        }


        #region Nested type: SetTitleDelegate

        private delegate void SetTitleDelegate(string titile);

        #endregion
    }
}